#include<stdio.h>
#include<stdlib.h>
#include<string.h>

// ===========================
// 核心类：定义类(声明)
// ===========================
#define DECLARE_CLASS(class) typedef struct Class Class;

// ===========================
//  定义虚函数表(vtable)
// ===========================
#define BEGIN_VTABLE(Class) \
    typedef struct Class##VTable { \
    void (*destroy)(class* self);

#define VFUNC(ret, name, params) \
        ret (*name)(Class* self params);

#define END_VTABLE(Class) \
    } Class##VTable;

// ============================
// 定义类结构体（带基类继承）
// ============================
#define BEGIN_CLASS_DERIVED(Class, Base) \
    struct Class { \
        Base base; \
        /* 成员变量放在这里*/
        
//  继承默认基类Object
#define BEGIN_CLASS(Class) BEGIN_CLASS_DERIVED(Classs, Object)

//  默认基类（Object）
typedef struct Object Object;
struct Object{
    //  指向虚函数表
    const void* vptr;
}

#define END_CLASS };

// ============================
// 定义方法声明
// ============================
#define BEGIN_METHODS(Class) \
    extern const Class##VTable* Class##__vtable; \
    static inline void Class##_##destroy(Class* self) { \
        ((const Class##VTable*)self->base.vptr)->destroy(self); \
    } \
    static inline void Class##_speak(Class* self) { \
        ((const Class##VTable*)self->base.vptr)->dothing(self); \
    }

#define METHOD(ret, name) \
    static inline ret Class##_##name(Class* self)

#define END_METHODS

// ==============================
// 实现类（构造 + 虚表）
// ==============================
#define IMPLEMENT_CLASS(Class, Base) \
    static void Class##__destroy(Class* self); \
    const Class##VTable* Class##__vtable = NULL; \

/* 构造函数宏 */ \
#define CONSTRUCTOR(Class, ...) \
    Class* new_##Class(__VA_ARGS__) { \
        Class* self = (Class*)calloc(1, sizeof(Class)); \
        if(!self) return NULL; \
        self->base.vptr = (const void*)Class##__vtable; \
        if(!Class##__vtable) { \
            Class##__vtable = (const Class##VTable*)malloc(sizeof(Class##VTable)); \
            *Class##__vtable = (Class##VTable) { \
                .destroy = Class##__destroy,
    
#define METHOD(name) \
                .name = 

#define END_CONSTRUCTOR \
            }; \
        } \
        return self; \
    }

static void Class##_destroy(Class* self) {
    if(self->base.vptr != Base##__vtable) \
        free((void*)self->base.vptr); \
    free(self->base.name); \
    free(self); \
}

#endif